package com.itheima.reggin.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.itheima.reggin.common.R;
import com.itheima.reggin.dto.OrdersDto;
import com.itheima.reggin.entity.OrderDetail;
import com.itheima.reggin.entity.Orders;
import com.itheima.reggin.entity.PageDto;
import com.itheima.reggin.entity.ShoppingCart;
import com.itheima.reggin.service.AddressBookService;
import com.itheima.reggin.service.OrderDetailService;
import com.itheima.reggin.service.OrdersService;
import com.itheima.reggin.service.ShoppingCartService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/order")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;
    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private ShoppingCartService shoppingCartService;



    @PostMapping("/again")
    public R<String> orderAgain(@RequestBody Orders orders, HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        //根据订单id查询订单
        orders = ordersService.getById(orders.getId());
        //通过订单id查询订单详情
        LambdaQueryWrapper<OrderDetail> lqw = new LambdaQueryWrapper<>();
        lqw.eq(OrderDetail::getOrderId,orders.getNumber());
        List<OrderDetail> orderDetails = orderDetailService.list(lqw);
        List<ShoppingCart> shoppingCarts = orderDetails.stream().map(orderDetail -> {
            ShoppingCart shoppingCart = new ShoppingCart();
            shoppingCart.setName(orderDetail.getName());
            shoppingCart.setImage(orderDetail.getImage());
            shoppingCart.setUserId(userId);
            shoppingCart.setDishId(orderDetail.getDishId());
            shoppingCart.setSetmealId(orderDetail.getSetmealId());
            shoppingCart.setDishFlavor(orderDetail.getDishFlavor());
            shoppingCart.setNumber(orderDetail.getNumber());
            shoppingCart.setAmount(orderDetail.getAmount());
            shoppingCart.setCreateTime(LocalDateTime.now());
            return shoppingCart;
        }).collect(Collectors.toList());
        shoppingCartService.saveBatch(shoppingCarts);
        return R.success();
    }
    /**
     * 提交订单
     * @param orders
     * @param session
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders, HttpSession session){
        return  ordersService.submit(orders,session);
    }

    @GetMapping("/userPage")
    public R<PageDto<OrdersDto>> userPage(PageDto<OrdersDto> pageDto, HttpSession session){

       return ordersService.userPage(pageDto, session);
    }

    /**
     * 订单明细信息
     * @param pageDto
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    public R<PageDto<Orders>> page(PageDto<Orders> pageDto,
                                   @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date beginTime,
                                   @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime){

        Page<Orders> ordersPage = new Page<>(pageDto.getPage(),  pageDto.getPageSize());
        //订单编号
        String orderNumber = pageDto.getName();



        //根据订单编号设置条件
        LambdaQueryWrapper<Orders> ordersLqw = new LambdaQueryWrapper<>();
        if (orderNumber!=null){
            ordersLqw.eq(Orders::getNumber,orderNumber);
        }
        //根据用户搜索的下单时间设置条件
        if (beginTime!=null && endTime!=null){
            ordersLqw.between(Orders::getOrderTime,beginTime,endTime);
        }
        //排序条件
        ordersLqw.orderByDesc(Orders::getOrderTime);

        ordersService.page(ordersPage,ordersLqw);
        List<Orders> orders = ordersPage.getRecords();

        pageDto.setRecords(orders);
        pageDto.setTotal((int) ordersPage.getTotal());



        return R.success(pageDto);


    }


    /**
     * 修改订单状态
     * @param orders
     * @return
     */
    @PutMapping
    public R<String> status(@RequestBody Orders orders){

        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Orders::getId,orders.getId());
        ordersService.update(orders,lqw);

        return R.success("修改成功");


    }
}
